package data;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;

import util.DBUtil;
import vo.UserEntity;
import dao.UserAddFindDao;

public class UserAddFindImpl implements UserAddFindDao {

	@Override
	public int addUser(UserEntity user) {
		int rs=0;
		String sql="insert into user_tb (username,pwd,orderfood,phoneNum,adress) values(?,?,?,?,?)";
		Connection con=DBUtil.connect();
		PreparedStatement pst=null;
		
		
		try {
			
			pst=con.prepareStatement(sql);
			pst.setString(1, user.getUsername());
			pst.setString(2, user.getPwd());
			pst.setString(3, user.getOrderfood());
			pst.setString(4, user.getPhoneNum());
			pst.setString(5, user.getAdress());
			rs=pst.executeUpdate();
		} catch (SQLException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		finally{
			DBUtil.free(con, pst, null);
		}
		
		
		return rs;
	
	}

	@Override
	public UserEntity findUser(String phoneNum, String pwd) {
		UserEntity s=null;
		String sql="select * from user_tb where phoneNum=? and pWd=?";
		Connection con=DBUtil.connect();
		PreparedStatement pst=null;
		ResultSet rs=null;
		
		try {
			pst=con.prepareStatement(sql);
			pst.setString(1, phoneNum);
			pst.setString(2, pwd);
			rs=pst.executeQuery();
			if(rs.next()){
				s=new UserEntity();
				s.setId(rs.getInt("id"));
				s.setUsername(rs.getString("username"));
				s.setOrderfood(rs.getString("orderfood"));
				s.setPhoneNum(rs.getString("phoneNum"));
				s.setAdress(rs.getString("adress"));
			}
		} catch (SQLException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		finally{
			DBUtil.free(con, pst, rs);
		}
		
		
		
		
		
		return s;
	}


}
